const http = require("http");
const fs = require("fs");

const server = http.createServer((request, response) => {
  //   console.log('__dirname',__dirname); // /Users/shenxuzhe/node/03-http/04-小练习
  const url = new URL(request.url, "http://127.0.0.1:9000");
  const pathname = url.pathname;
  console.log("url", url);
  if (pathname == "/") {
    const html = fs.readFileSync(__dirname + "/index.html"); // buffer
    response.end(html); // 支持字符串与buffer
  } else if (pathname == "/index.css") {
    const html = fs.readFileSync(__dirname + "/index.css"); // buffer
    response.end(html); // 支持字符串与buffer
  } else {
    response.statusCode = 404;
    response.end("404 no found"); // 支持字符串与buffer
  }
});

server.listen(9000, () => {
  console.log("服务启动了");
});
